package cn.rongdao.jrkabao.widget;

import android.app.Activity;
import android.content.pm.PackageManager;
import android.support.v4.content.ContextCompat;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.mifengkong.frtools.inputmethod.FRView;
import com.mifengkong.frwidget.dialog.FRDialog;
import com.mifengkong.frwidget.popup_window.base.SlideFromBottomBasePopupMenu;

import cn.rongdao.jrkabao.R;
import cn.rongdao.jrkabao.ui.cashadvance.uicomponent.DialogFromBottomHeader;
import cn.rongdao.jrkabao.util.CommonUtil;

/**
 * Created by jack on 2017/9/12
 */

public class SelectPaymentWayFromBottom implements View.OnClickListener {

    private SlideFromBottomBasePopupMenu mSlideFromBottomBasePopupMenu;

    private TextView tvSetNotRepayment;
    private TextView tvSetRepayment;
    private TextView tvZhiFuBao;
    private TextView tvWeiXin;
    private boolean isRepayment;
    private Activity activity;
    private OnCheckBalanceClickListener onCheckBalanceClickListener;
    private OnSelectListener onSelectListener;
    private boolean isClick;

    public SelectPaymentWayFromBottom(Activity activity, boolean isRepayment) {
        this.activity = activity;
        View itemView = FRView.inflateLayout(activity, R.layout.dialog_setect_payment_way_card);
        DialogFromBottomHeader dialogFromBottomHeader = (DialogFromBottomHeader) itemView.findViewById(R.id.dspwc_select_card);
        this.isRepayment = isRepayment;
        tvSetNotRepayment = (TextView) itemView.findViewById(R.id.dspwc_tv_set_not_payment);
        tvSetRepayment = (TextView) itemView.findViewById(R.id.dspwc_tv_set_payment);
        TextView tvTip = (TextView) itemView.findViewById(R.id.dspwc_tv_tip);
        LinearLayout llTip = (LinearLayout) itemView.findViewById(R.id.dspwc_ll_repayment);
        tvZhiFuBao = (TextView) itemView.findViewById(R.id.dspwc_tv_zhifubao_payment);
        tvWeiXin = (TextView) itemView.findViewById(R.id.dspwc_tv_weixin_payment);

        setZhiFuBaoContent(isRepayment);
        setWeiXinContent(isRepayment);
        tvTip.setVisibility(isRepayment ? View.VISIBLE : View.GONE);
        llTip.setVisibility(isRepayment ? View.VISIBLE : View.GONE);

        mSlideFromBottomBasePopupMenu = new SlideFromBottomBasePopupMenu(activity);
        mSlideFromBottomBasePopupMenu.setMenuItems(itemView);

        dialogFromBottomHeader.setNameAndOnClickListener(isRepayment ? "选择还款方式" : "查余额", new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });

        initListener();
    }

    private void initListener() {
        tvSetNotRepayment.setOnClickListener(this);
        tvSetRepayment.setOnClickListener(this);
        tvZhiFuBao.setOnClickListener(this);
        tvWeiXin.setOnClickListener(this);
    }

    public void setRepaymentIsSelect(boolean isSelect) {
        setTvRepaymentIsSelect(isSelect);

    }

    private void setTvRepaymentIsSelect(boolean isSelect) {
        tvSetRepayment.setCompoundDrawablesWithIntrinsicBounds(isSelect ? R.drawable.icon_radio_select : R.drawable.icon_radio_un_select, 0, 0, 0);
        tvSetNotRepayment.setCompoundDrawablesWithIntrinsicBounds(isSelect ? R.drawable.icon_radio_un_select : R.drawable.icon_radio_select, 0, 0, 0);
        tvSetNotRepayment.setEnabled(isSelect);
        tvSetRepayment.setEnabled(!isSelect);
        if (null != onSelectListener && isClick) {
            onSelectListener.onSelect(isSelect);
            isClick = false;
        }
    }

    private void setZhiFuBaoContent(boolean isRepayment) {
        if (isRepayment) {
            tvZhiFuBao.setText("支付宝还款");
            tvZhiFuBao.setCompoundDrawablesWithIntrinsicBounds(R.drawable.icon_zhifubao_payment, 0, 0, 0);
        } else {
            tvZhiFuBao.setText("邮箱查询");
            tvZhiFuBao.setCompoundDrawablesWithIntrinsicBounds(R.drawable.icon_check_bill_email, 0, 0, 0);
        }
    }

    private void setWeiXinContent(boolean isRepayment) {
        if (isRepayment) {
            tvWeiXin.setText("微信还款");
            tvWeiXin.setCompoundDrawablesWithIntrinsicBounds(R.drawable.icon_weixin_payment, 0, 0, 0);
        } else {
            tvWeiXin.setText("短信查询");
            tvWeiXin.setCompoundDrawablesWithIntrinsicBounds(R.drawable.icon_check_bill_msg, 0, 0, 0);
        }
    }

    public void dismiss() {
        if (null != mSlideFromBottomBasePopupMenu) {
            mSlideFromBottomBasePopupMenu.dismiss();
        }
    }

    public void show() {
        if (null != mSlideFromBottomBasePopupMenu) {
            mSlideFromBottomBasePopupMenu.showPopupWindow();
        }
    }

    private void showDialog(String content) {
        new FRDialog.MDBuilder(activity)
                .setMessage(content)
                .setNegativeContentAndListener("知道了", null)
                .setNegativeTextColor(ContextCompat.getColor(activity, R.color.colorPrimary))
                .show();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.dspwc_tv_set_not_payment:
                isClick = true;
                setTvRepaymentIsSelect(false);
                break;
            case R.id.dspwc_tv_set_payment:
                isClick = true;
                setTvRepaymentIsSelect(true);
                break;
            case R.id.dspwc_tv_zhifubao_payment:
                if (isRepayment) {
                    if (!CommonUtil.isMobileInstall(CommonUtil.A_LI_PAY_PACKAGE)) {
                        showDialog("请安装支付宝客户端");
                        return;
                    }
                    try {
                        CommonUtil.openAppByPackageName(activity, CommonUtil.A_LI_PAY_PACKAGE);
                    } catch (PackageManager.NameNotFoundException e) {
                        e.printStackTrace();
                    }
                } else {
                    if (null != onCheckBalanceClickListener) {
                        onCheckBalanceClickListener.emailCheck();
                    }
                }
                break;
            case R.id.dspwc_tv_weixin_payment:
                if (isRepayment) {
                    if (!CommonUtil.isMobileInstall(CommonUtil.WEI_XIN_PAY_PACKAGE)) {
                        showDialog("请安装微信客户端");
                        return;
                    }
                    try {
                        CommonUtil.openAppByPackageName(activity, CommonUtil.WEI_XIN_PAY_PACKAGE);
                    } catch (PackageManager.NameNotFoundException e) {
                        e.printStackTrace();
                    }
                } else {
                    if (null != onCheckBalanceClickListener) {
                        onCheckBalanceClickListener.msgCheck();
                    }
                }
                break;
        }
    }

    public void setOnCheckBalanceClickListener(OnCheckBalanceClickListener onCheckBalanceClickListener) {
        this.onCheckBalanceClickListener = onCheckBalanceClickListener;
    }

    public void setOnSelectListener(OnSelectListener onSelectListener) {
        this.onSelectListener = onSelectListener;
    }

    public interface OnSelectListener {
        void onSelect(boolean isSelected);
    }

    public interface OnCheckBalanceClickListener {
        void emailCheck();

        void msgCheck();
    }
}